/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import java.util.List;

import entity.Teacher;

import javax.annotation.Resource;
import javax.annotation.Resource.AuthenticationType;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;

/**
 *
 * @author gio
 */
//@Resource(name = "jdbc/SQLDB-83", type = javax.sql.DataSource.class, shareable = true, authenticationType = AuthenticationType.CONTAINER)
public class TeacherFacade{
    
	@PersistenceUnit(unitName = "WebPortalDB2")
	EntityManagerFactory emf;	


    public TeacherFacade() {
    }
    
    
	public void create(Teacher entity) {
		System.out.println("Metodo create");
		EntityManager em = emf.createEntityManager();
		em.persist(entity);
		em.close();
		System.out.println("create: Teacher successfuly inserted");
	}

	public void edit(Teacher entity) {
		EntityManager em = emf.createEntityManager();	
		em.merge(entity);
		em.close();
		System.out.println("Teacher successfuly edited");
	}

	public void remove(Teacher entity) {
		EntityManager em = emf.createEntityManager();
		em.remove(em.merge(entity));
		em.close();
		System.out.println("Teacher successfuly removed");
	}

	public Teacher find(Object id) {
		Teacher res = null;
		EntityManager em = emf.createEntityManager();
		res = em.find(Teacher.class, id);
		return res;
	}
	
	public List<Teacher> findAll() {
		EntityManager em = emf.createEntityManager();
		javax.persistence.criteria.CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
		cq.select(cq.from(Teacher.class));
		return em.createQuery(cq).getResultList();
	}
    
    
}
